﻿using System;

namespace SpiderSync
{
    enum MessageType { error, warning, info, message }

    static class Output
    {
        private static bool _verbose;
        public static bool Verbose
        {
            get { return _verbose; }
            set { _verbose = value; }
        }

        private static char[] _indicator = {'-', '\\', '|', '/'};
        private static short _indicatorPos = 0;

        private static bool _indicatorIsActive = false;

        public static void Write(string message)
        {
            Write(message, MessageType.info);
        }

        public static void Write(string message, MessageType type)
        {     
            if (_verbose || type == MessageType.error || type == MessageType.message)
            {
                ClearLine();
                Console.ForegroundColor = GetTypeColor(type);
                Console.WriteLine(message);
                Console.ResetColor();
                _indicatorIsActive = false;
            }
        }

        public static void ShowIndicator()
        {
            if (!_indicatorIsActive)
            {
                ClearLine();
            }
            Console.ForegroundColor = ConsoleColor.Green;
            Console.Write("\rSyncing... " + _indicator[_indicatorPos]);
            Console.ResetColor();
            _indicatorIsActive = true;
        }

        public static void ShowIdle()
        {
            ClearLine();
            Console.ForegroundColor = ConsoleColor.Green;
            Console.Write("Idle...");
            Console.ResetColor();
            _indicatorIsActive = false;
        }

        public static void PulseIndicator()
        {
            if (++_indicatorPos >= _indicator.Length)
            {
                _indicatorPos = 0;
            }
            ShowIndicator();
        }

        private static void ClearLine()
        {
            Console.SetCursorPosition(0, Console.CursorTop);
            Console.Write(new String(' ', Console.BufferWidth - 1));
            Console.SetCursorPosition(0, Console.CursorTop);
        }

        private static ConsoleColor GetTypeColor(MessageType type)
        {
            switch (type)
            {
                case MessageType.error:
                    return ConsoleColor.DarkRed;

                case MessageType.warning:
                    return ConsoleColor.DarkYellow;

                case MessageType.info:
                case MessageType.message:
                default:
                    return ConsoleColor.DarkGreen;
            }
        }
    }
}
